package com.e3.basdoc.corp.service.impl;

import java.util.List;

import com.e3.basdoc.corp.dao.CorpDAO;
import com.e3.basdoc.corp.entity.CorpEntity;
import com.e3.basdoc.corp.model.CorpModel;
import com.e3.basdoc.corp.service.CorpService;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;
import com.e3.web.core.model.SuperModel;

public class CorpServiceImpl extends SuperService implements CorpService {

	private CorpDAO corpDAO;

	private CorpDAO getCorpDAO() {
		if (corpDAO == null) {
			corpDAO = DAOHelper.createDAOInstance(CorpDAO.class);
		}
		return corpDAO;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpService#newCorp(com.e3.basdoc.corp.model.CorpModel)
	 */
	public CorpModel newCorp(CorpModel corpModel) throws ServiceException {
		validateCorp(corpModel);
		checkCorpExisted(corpModel);

		CorpEntity corp = corpModelToEntity(corpModel, null);
		corpModel.setId(corp.getId());

		getCorpDAO().persist(corp);

		return corpModel;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpService#modifyCorp(com.e3.basdoc.corp.model.CorpModel)
	 */
	public CorpModel modifyCorp(CorpModel corpModel) throws ServiceException {
		validateCorp(corpModel);

		long id = corpModel.getId();
		CorpEntity entity = getCorpDAO().getById(CorpEntity.class, id);
		if (entity == null) {
			newCorp(corpModel);
			return corpModel;
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(corpModel.getCode())) {
			checkCorpExisted(corpModel);
		}

		corpModelToEntity(corpModel, entity);

		getCorpDAO().merge(entity);

		return corpModel;
	}

	private void validateCorp(CorpModel corpModel) throws ServiceException {
		String code = corpModel.getCode();
		String name = corpModel.getName();

		if (code == null || code.trim().length() == 0 || name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置公司的编码和名称。");
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpService#checkCorpExisted(com.e3.basdoc.corp.model.CorpModel)
	 */
	public void checkCorpExisted(CorpModel corpModel) throws ServiceException {
		if (corpModel == null) {
			return;
		}

		String code = corpModel.getCode();
		String name = corpModel.getName();
		CorpEntity entity = getCorpDAO().getCorpByCode(code);
		if (entity != null) {
			throw new ServiceException("编码为'" + code + "',名称为'" + name + "'已经存在，请设置新的编码和名称。");
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.corp.service.impl.CorpService#deleteCorps(java.util.List)
	 */
	public void deleteCorps(List<CorpModel> corpModels) throws ServiceException {
		if (corpModels == null || corpModels.isEmpty()) {
			return;
		}
		Long[] ids = getIds(corpModels);
		getCorpDAO().deleteCorpByIds(ids);
		getCorpDAO().deleteCorpChildren(ids);
	}

	private Long[] getIds(List<? extends SuperModel> models) {
		if (models == null || models.isEmpty()) {
			return null;
		}
		Long[] ids = new Long[models.size()];
		for (int i = 0; i < models.size(); i++) {
			SuperModel model = models.get(i);
			ids[i] = model.getId();
		}
		return ids;
	}

	private CorpEntity corpModelToEntity(CorpModel corpModel, CorpEntity entity) {
		CorpEntity corp = super.modelToEntity(CorpEntity.class, corpModel, entity);

		// 基本信息
		corp.setCode(corpModel.getCode());
		corp.setName(corpModel.getName());
		corp.setShortName(corpModel.getShortName());
		corp.setType(Integer.parseInt(corpModel.getType()));

		corp.setParentId(corpModel.getParentId());
		corp.setRegionId(corpModel.getRegionId());

		corp.setBusinessAddress(corpModel.getBusinessAddress());
		corp.setContactAddress(corpModel.getContactAddress());
		corp.setTelphone(corpModel.getTelphone());
		corp.setEmail(corpModel.getEmail());
		corp.setZipcode(corpModel.getZipcode());
		corp.setFax(corpModel.getFax());
		corp.setRemark(corpModel.getRemark());

		// 注册信息
		corp.setRegisterAddress(corpModel.getRegisterAddress());
		corp.setLegalPerson(corpModel.getLegalPerson());
		corp.setLegalPersonIdcard(corpModel.getLegalPersonIdcard());
		corp.setRegisterNo(corpModel.getRegisterNo());
		corp.setContactPerson(corpModel.getContactPerson());
		corp.setContactWay(corpModel.getContactWay());
		corp.setFoundTime(corpModel.getFoundTime().getTime());
		corp.setRegisterStartTime(corpModel.getRegisterStartTime().getTime());
		corp.setRegisterEndTime(corpModel.getRegisterEndTime().getTime());
		
		corp.setCurrencyId(corpModel.getCurrencyId());
		corp.setTaxpayerType(Integer.parseInt(corpModel.getTaxpayerType()));

		// 其他信息
		corp.setEstablishAccount(corpModel.getEstablishAccount());
		corp.setMaxUsers(Integer.parseInt(corpModel.getMaxUsers()));
		corp.setMaxBranchs(Integer.parseInt(corpModel.getMaxBranchs()));
		corp.setStartTime(corpModel.getStartTime().getTime());
		corp.setEndTime(corpModel.getEndTime().getTime());

		return corp;
	}

}
